{**************************************************}
{ }
{ Number to letters unit version 1.0 }
{ }
{ copyright (C) Dylan Thomas 2000 }
{ }
{ License: No significant restrictions. }
{ }
{ Language: US. English }
{ }
{**************************************************}

unit NumberToLetters;

interface

{var

Calls: Integer;} //Use to keep track of the number of recursive calls

(* This function returns the written equivalent of a number. *)
function NumToLetters(number:real):string;

implementation
uses SysUtils;

type

TNumberStr = string[13];

const
Numbers: array[1..19] of TNumberStr = ('ONE', 'TWO', 'THREE', 'FOUR',
'FIVE', 'SIX', 'SEVEN', 'EIGHT', 'NINE', 'TEN', 'ELEVEN', 'TWELVE',
'THIRTEEN', 'FOURTEEN', 'FIFTEEN', 'SIXTEEN', 'SEVENTEEN', 'EIGHTEEN',
'NINETEEN');

Tenths: array[1..9] of TNumberStr = ('TEN', 'TWENTY', 'THIRTY', 'FORTY',
'FIFTY', 'SIXTY', 'SEVENTY', 'EIGHTY', 'NINETY');

ErrorString = 'not in valid range';

Min = 1.00;
Max = 4294967295.99;

function NumToLetters(number:real):string;

function RecurseNumber(N: LongWord): string;
begin
{Inc(Calls);} //Use to keep track of the number of recursive calls
case N of
1..19:
Result := Numbers[N];
20..99:
Result := Tenths[N div 10] +' '+ RecurseNumber(N mod 10);
100..999:
if N mod 100 = 0 then
Result := Numbers[N div 100] + ' HUNDRED'
else
Result := Numbers[N div 100] + ' HUNDRED AND ' + RecurseNumber(N mod 100);
1000..999999:
Result := RecurseNumber(N div 1000) + ' THOUSAND ' +
RecurseNumber(N mod 1000);
1000000..999999999: Result := RecurseNumber(N div 1000000) + ' MILLION '
+ RecurseNumber(N mod 1000000);
1000000000..4294967295: Result := RecurseNumber(N div 1000000000) +
' BILLION ' + RecurseNumber(N mod 1000000000);
end; {Case N of}
end; {RecurseNumber}

begin
{Calls := 0;} //Use to keep track of the number of recursive calls
if (Number >= Min) and (Number <= Max) then
begin
Result := RecurseNumber(Round(Int(Number)));
{Added for cents in a currency value}
if not(Frac(Number) = 0.00) then
Result := Result + ' AND ' + RecurseNumber(Round(Frac(Number) * 100)) + ' CENTS';
///100;
end
else
  Result := ErrorString;
//raise ERangeError.CreateFmt(%g + ErrorString + %g..%g,
//[Number, Min, Max]);
end;{NumToLetters}

end.
